﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;
namespace HFWebManage.Subwebsite
{
    public partial class AddSubwebsite : HFController.BagePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //绑定省份城市
                DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
                this.bindProvinceID();

                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        SubwebsiteController.getController().doDelegeSubweb(ID);
                        break;
                }
            }
        }

        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            
            //得到这条记录
            HFModel.VO.SubwebsiteModel model = SubwebsiteController.getController().getModel(ID);

            lblUrl.Text = model.Url;

            hf.Model.City city = CityController.getController().getCityList().Find(i => i.ID == model.CityID);

            DropProvinceID.SelectedValue = CityController.getController().getProvinceList().Find(i => i.ID == city.ProvinceID).ID.ToString();

            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));

            DropCityID.SelectedValue = model.CityID.ToString();

            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }

        public void bindProvinceID()
        {
            DropProvinceID.DataSource = CityController.getController().getProvinceList(100);
            DropProvinceID.DataValueField = "ID";
            DropProvinceID.DataTextField = "ProvinceName";
            DropProvinceID.DataBind();
            DropProvinceID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        public void bindCityID()
        {
            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                //修改新闻
                SubwebsiteController.getController().doModifySubweb(int.Parse(DropCityID.SelectedValue),
                    DropCityID.SelectedItem.Text, int.Parse(IDS.Value) , lblUrl.Text);
            }
            else
            {
                //添加新闻
                SubwebsiteController.getController().doAddSubweb(int.Parse(DropCityID.SelectedValue),
                    DropCityID.SelectedItem.Text, lblUrl.Text);
            }
        }

        /// <summary>
        /// 选择联动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DropProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bindCityID();
        }
    }
}